package com.ffcs.crmd.tsp.server.jdbc.service.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobIdentityInfo;
import com.ffcs.crmd.tsp.server.jdbc.repository.JobIdentityInfoRepository;
import com.ffcs.crmd.tsp.server.jdbc.service.IJobIdentityInfoService;

/**
 * 
 * 功能说明:任务服务
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class JobIdentityInfoServiceImpl implements IJobIdentityInfoService {
    
    private JobIdentityInfoRepository jobIdentityInfoRepository = new JobIdentityInfoRepository();;
    
    public JobIdentityInfoServiceImpl() {
        
    }
    
    @Override
    public JobIdentityInfo get(Connection conn, JobIdentityInfo jobIdentityInfo) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(jobIdentityInfo.getTaskIdentityId());
        params.add(jobIdentityInfo.getJobId());
        params.add(jobIdentityInfo.getId());
        return jobIdentityInfoRepository.query(GET_JOB_IDENTITY_INFO, params, conn);
    }
    
    @Override
    public int querySize(Connection conn, JobIdentityInfo jobIdentityInfo) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtp(SELECT_JOB_IDENTITY_INFO, jobIdentityInfo, params, true);
        return jobIdentityInfoRepository.queryRows(sql, params, conn);
    }
    
    @Override
    public List<JobIdentityInfo> query(Connection conn, JobIdentityInfo jobIdentityInfo) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtp(SELECT_JOB_IDENTITY_INFO, jobIdentityInfo, params, false);
        return jobIdentityInfoRepository.queryList(sql, params, conn);
    }
    
    @Override
    public List<JobIdentityInfo> queryPage(Connection conn, JobIdentityInfo jobIdentityInfo) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtpPage(SELECT_JOB_IDENTITY_INFO, jobIdentityInfo, params);
        return jobIdentityInfoRepository.queryList(sql, params, conn);
    }
    
    @Override
    public boolean add(Connection conn, JobIdentityInfo jobIdentityInfo) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(TspSeqServiceImpl.getSeq());
        params.add(jobIdentityInfo.getJobId());
        params.add(jobIdentityInfo.getTaskIdentityId());
        params.add(jobIdentityInfo.getClusterName());
        params.add(jobIdentityInfo.getIdentity());
        params.add(jobIdentityInfo.getDescription());
        return jobIdentityInfoRepository.execute(INSERT_JOB_IDENTITY_INFO, params, conn);
    }
    
    @Override
    public boolean delete(Connection conn, JobIdentityInfo jobIdentityInfo) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtpDelete(DELETE_JOB_IDENTITY_INFO, jobIdentityInfo, params);
        return jobIdentityInfoRepository.execute(sql, params, conn);
    }
    
    @Override
    public boolean update(Connection conn, JobIdentityInfo jobIdentityInfo) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(jobIdentityInfo.getIdentity());
        params.add(jobIdentityInfo.getId());
        return jobIdentityInfoRepository.execute(UPDATE_JOB_IDENTITY_INFO, params, conn);
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param jobIdentityInfo
     * @param params
     * @return
     */
    private String rtp(String sql, JobIdentityInfo jobIdentityInfo, final List<Object> params, boolean isLike) {
        if (StringUtils.isNoneBlank(jobIdentityInfo.getIdentity())) {
            if (isLike) {
                sql += " AND " + COL_JOB_IDENTITY_INFO_IDENTITY + " LIKE ?";
                params.add("%" + jobIdentityInfo.getIdentity() + "%");
            } else {
                sql += " AND " + COL_JOB_IDENTITY_INFO_IDENTITY + " = ?";
                params.add(jobIdentityInfo.getIdentity());
            }
        }
        
        if (jobIdentityInfo.getJobId() != null) {
            sql += " AND " + COL_JOB_IDENTITY_INFO_JOB_ID + " = ?";
            params.add(jobIdentityInfo.getJobId());
        }
        
        if (jobIdentityInfo.getTaskIdentityId() != null) {
            sql += " AND " + COL_JOB_IDENTITY_INFO_TASK_IDENTITY_ID + " = ?";
            params.add(jobIdentityInfo.getTaskIdentityId());
        }
        return sql;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param jobIdentityInfo
     * @param params
     * @return
     */
    private String rtpPage(String sql, JobIdentityInfo jobIdentityInfo, final List<Object> params) {
        String rsql = rtp(sql, jobIdentityInfo, params, true);
        rsql += " LIMIT ?,?";
        params.add(jobIdentityInfo.getPageNumber());
        params.add(jobIdentityInfo.getPageSize());
        return rsql;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param jobIdentityInfo
     * @param params
     * @return
     * @throws TspException 
     */
    private String rtpDelete(String sql, JobIdentityInfo jobIdentityInfo, final List<Object> params) throws TspException {
        if (jobIdentityInfo.getId() == null && jobIdentityInfo.getTaskIdentityId() == null && jobIdentityInfo.getJobId() == null) {
            throw new TspException("delete job ['id' or 'taskIdentityId' or 'jobId'] can't be null at the same time！");
        }
        
        if (jobIdentityInfo.getId() != null) {
            sql += " AND " + COL_ID + " = ?";
            params.add(jobIdentityInfo.getId());
        }
        
        if (jobIdentityInfo.getJobId() != null) {
            sql += " AND " + COL_JOB_IDENTITY_INFO_JOB_ID + " = ?";
            params.add(jobIdentityInfo.getJobId());
        }
        
        if (jobIdentityInfo.getTaskIdentityId() != null) {
            sql += " AND " + COL_JOB_IDENTITY_INFO_TASK_IDENTITY_ID + " = ?";
            params.add(jobIdentityInfo.getTaskIdentityId());
        }
        return sql;
    }
    
}
